package com.station.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.station.Activitys.bean.MyRelease;
import com.station.Activitys.bean.State;
import com.station.Activitys.bean.pack.UserJsonPack;
import com.station.Activitys.http.ApiService;
import com.station.Activitys.http.NetWorkMgr;
import com.station.Activitys.http.api.UserAPI;
import com.station.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 项目名称：
 * 类描述：我的 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2018/5/9 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2018/5/9 0027 下午 1:33
 * 修改备注：
 */
public class UserManager {

    private static UserAPI mUserAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class UserApiManagerHolder {
        private static final UserManager INSTANCE = new UserManager();
    }
    public static final UserManager getUserApiManager(Activity activity, int taskId) {
        mUserAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(UserAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return UserApiManagerHolder.INSTANCE;
    }

    public void getType(int type){
        RefType=type;
    }

    /**
     * 登录
     */
    public void userLogin(String userName, String Pwd) {
        Subscription subscription = mUserAPI
                .userLogin(userName,Pwd).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserJsonPack>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                        //log 打印
                        EventBus.getDefault().post(e.toString(), Constant.BUS_USER_LOGIN_ERROR);
                    }
                    @Override
                    public void onNext(UserJsonPack pack) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(pack, Constant.BUS_USER_LOGIN);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 我的信息中心
     */
    public void userReleaseList(int UserId,String startTime,String EndTime,String Title,
                                 int pageIndex, int pageSize) {
        Subscription subscription = mUserAPI
                .userReleaseList(UserId, startTime, EndTime, Title, pageIndex, pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MyRelease>>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                        //log 打印
                        EventBus.getDefault().post(e.toString(), Constant.BUS_USER_RELEASE_ERROR);
                    }
                    @Override
                    public void onNext(List<MyRelease> list) {
                        if(RefType == Constant.FIRST_LOAD){
                            //返回成功 数据处理
                            EventBus.getDefault().post(list, Constant.BUS_USER_RELEASE);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_USER_RELEASE_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    /**
     * 修改登录密码
     */
    public void userUpdatePwd(int userId,String oldPwd, String newPwd) {
        Subscription subscription = mUserAPI
                .userUpdatePwd(userId, oldPwd, newPwd).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<State>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        //加载出错,可用于 “页面” 提示信息
                        EventBus.getDefault().post("onCompleted", Constant.BUS_MY_UPDATE_PWD_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(), "structureList 错误" + e.toString());
                    }

                    @Override
                    public void onNext(State bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_MY_UPDATE_PWD);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 修改登录密码
     */
    public void userUpdateMsg(int userId,String ImgUrl, String RealName, String Job) {
        Subscription subscription = mUserAPI
                .userUpdateMsg(userId, ImgUrl, RealName , Job).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<State>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        //加载出错,可用于 “页面” 提示信息
                        EventBus.getDefault().post("onCompleted", Constant.BUS_MY_UPDATE_MSG_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(), "structureList 错误" + e.toString());
                    }

                    @Override
                    public void onNext(State bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_MY_UPDATE_MSG);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

}
